package com.pk.platform.util;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.pk.platform.business.data.service.IBaseDataService;
import com.pk.platform.business.privilege.service.IPrivilegeService;
import com.pk.platform.domain.data.BaseData;
import com.pk.platform.domain.privilege.Privilege;
import com.pk.platform.util.constant.Constant;

public class GlobalLoader implements Servlet {

	private Logger log = Logger.getLogger(GlobalLoader.class);
	
	private static ServletContext context;
	
	private IPrivilegeService privilegeService;
	
	private IBaseDataService baseDataService;
	
	private ApplicationContext ctx;
	
	public void destroy() {
		
	}

	public ServletConfig getServletConfig() {
		return null;
	}

	public String getServletInfo() {
		return null;
	}

	public void init(ServletConfig config) throws ServletException {
		context = config.getServletContext();
		ctx = WebApplicationContextUtils.getWebApplicationContext(context);
		privilegeService = (IPrivilegeService)ctx.getBean("privilegeService");
		baseDataService = (IBaseDataService)ctx.getBean("baseDataService");
		try
		{
			System.out.println("--开始加载数据");
			this.loadData();
			System.out.println("--数据加载成功");
		}
		catch (Exception e)
		{
			log.error("--数据加载异常", e);
		}
	}

	public void service(ServletRequest req, ServletResponse res)
			throws ServletException, IOException {
		
	}
	
	//加载数据
	private void loadData()
	{
		context.setAttribute(Constant.CONTEXT_PATH, context.getContextPath()); //系统路径
		context.setAttribute(Constant.ACTION_MAP, this.loadSysMenu()); //系统所有菜单
		context.setAttribute(Constant.IS_DEBUG, this.loadIsDebug()); //是否调试信息
		context.setAttribute("baseDataService", this.baseDataService); //是否调试信息
	}
	
	//加载系统所有菜单
	private Map<String, String> loadSysMenu()
	{
		List<Privilege> privilegeList = privilegeService.querySysPrivilege();
		Map<String, String> actionMap = new HashMap<String, String>();
		for(Privilege p : privilegeList)
		{
			String url = p.getUrl();
			if(url != null)
			{
				int index = url.indexOf("?");
				if(index > -1)
				{
					url = url.substring(0, index);
				}
				actionMap.put(url, "");
			}
		}
		return actionMap;
	}
	
	//加载是否打印调试信息
	private String loadIsDebug()
	{
		BaseData bd = baseDataService.queryBaseDataById(Constant.IS_DEBUG);
		if(bd != null && Constant.N.equals(bd.getDataValue()))
		{
			return Constant.N;
		}
		return Constant.Y;
	}

	public static ServletContext getContext() {
		return context;
	}

	public static void setContext(ServletContext context) {
		GlobalLoader.context = context;
	}

}
